package vn.bk.legends.alarmtraffic;

import vn.bk.legends.alarmtrafic.R;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class SplashScreen extends FragmentActivity {
    private boolean isRunning;
    private LocationManager locationManager;
    private LocationListener locationListener;

    // GPS

    public static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in
    // Meters
    public static final long MINIMUM_TIME_BETWEEN_UPDATES = 0; // in

    public static double latitude = 21.02784;

    public static double longtitude = 105.852284;
    public static boolean isDefault = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.splash_screen);

    }

    @Override
    protected void onStart() {
	// TODO Auto-generated method stub
	super.onStart();

	Thread background = new Thread() {
	    @Override
	    public void run() {
		try {
		    for (int i = 0; i < 10 && isRunning; i++) {
			Thread.sleep(100);
		    }
		} catch (InterruptedException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		} finally {
		    detectLocation();
		}
	    }
	};
	isRunning = true;
	background.start();
    }

    @Override
    protected void onStop() {
	// TODO Auto-generated method stub
	super.onStop();
	isRunning = false;
    }

    @Override
    protected void onPause() {
	// TODO Auto-generated method stub
	super.onPause();
	finish();
    }

    public void detectLocation() {
	locationManager = (LocationManager) this
		.getSystemService(Context.LOCATION_SERVICE);

	locationListener = new LocationListener() {

	    @Override
	    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

	    }

	    @Override
	    public void onProviderEnabled(String arg0) {
	    }

	    @Override
	    public void onProviderDisabled(String arg0) {

	    }

	    @Override
	    public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub

	    }

	};

	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
		MINIMUM_TIME_BETWEEN_UPDATES,
		MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
	if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
	    findNetwork();

	} else if (locationManager
		.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
	    findGPS();
	} else {

	}
    }

    private void findNetwork() {
	Log.i("LOCATION", "NETWORK");
	try {
	    locationManager.requestLocationUpdates(
		    LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	    Location lastKnowLocation = locationManager
		    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	    if (lastKnowLocation != null) {
		setGeoLocation(lastKnowLocation);
	    } else {

		if (locationManager
			.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
		    findGPS();
		}
	    }
	} catch (IllegalArgumentException e) {
	    e.printStackTrace();
	}
    }

    private void findGPS() {
	Log.i("LOCATION", "GPS");
	try {
	    locationManager.requestLocationUpdates(
		    LocationManager.GPS_PROVIDER, 0, 0, locationListener);

	    Location lastKnowLocation = locationManager
		    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
	    if (lastKnowLocation != null) {
		Log.v("TEST", "last lat: " + lastKnowLocation.getLatitude());
		setGeoLocation(lastKnowLocation);

	    }
	} catch (IllegalArgumentException e) {

	}
    }

    private void setGeoLocation(Location location) {
	latitude = location.getLatitude();
	longtitude = location.getLongitude();

	isDefault = false;
	startActivity(new Intent(SplashScreen.this, MainActivity.class));
    }

}
